package com.cloudkinto.appletservice.orderwork.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.orderwork.AppletOrderWorkService;
import com.cloudkinto.appletservice.orderwork.vo.*;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.utils.OrderCodeUtil;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.orderwork.vo.OrderWorkAddReq;
import com.cloudkinto.service.orderwork.vo.OrderWorkPriceDetail;
import com.cloudkinto.service.orderwork.vo.OrderWorkProductDetail;
import com.cloudkinto.service.orderwork.vo.OrderWorkUpdateReq;
import com.cloudkinto.service.shop.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-06-18
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletOrderWorkServiceImpl extends ServiceImpl<OrderWorkDao, OrderWorkDo> implements AppletOrderWorkService {
    @Autowired
    private OrderWorkDao dao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private DicBusinessItemDao dicBusinessItemDao;
    @Autowired
    private OrderWorkPriceDao orderWorkPriceDao;
    @Autowired
    private OrderWorkProductDao orderWorkProductDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private ShopService shopService;

    @Override
    public AppletOrderWordDetailRes detail(Long id) {
        OrderWorkDo entityDo = dao.selectById(id);
        AppletOrderWordDetailRes res = new AppletOrderWordDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res, entityDo);
    }

    //查询明细之后要执行的业务定制处理
    private AppletOrderWordDetailRes handlerDetailAfter(AppletOrderWordDetailRes res, OrderWorkDo orderWorkDo) {
        ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
        if (shopDo != null) {
            res.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
        if (storageDo != null) {
            res.setStorageName(storageDo.getStorageName());
        }
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
        if (businessItemDo != null) {
            res.setWorkTypeName(businessItemDo.getDicItemValue());
        }
        res.setStatusName(getStatusStr(orderWorkDo.getStatus()));

        res.setContent(orderWorkDo.getContent());
        res.setSysTips(orderWorkDo.getSysTips());
//        res.setFileUrl(fileService.getFilePathMulti(orderWorkDo.getFileId()));

        List<OrderWorkPriceDetail> priceDetailList = new ArrayList<>();
        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, orderWorkDo.getId());
        List<OrderWorkPriceDo> orderWorkPriceDoList = orderWorkPriceDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(orderWorkPriceDoList)) {
            orderWorkPriceDoList.forEach(orderWorkPriceDo -> {
                OrderWorkPriceDetail priceDetail = new OrderWorkPriceDetail();
                priceDetail.setName(orderWorkPriceDo.getName());
                priceDetail.setPrice(orderWorkPriceDo.getPrice()+(orderWorkPriceDo.getCurrency().intValue()==0?"(RMB)":"(JPY)"));
                priceDetailList.add(priceDetail);
            });
        }
        res.setPriceDetailList(priceDetailList);

//        List<OrderWorkProductDetail> productDetailList = new ArrayList<>();
//        LambdaQueryWrapper<OrderWorkProductDo> productWrapper = new LambdaQueryWrapper<>();
//        productWrapper.eq(OrderWorkProductDo::getOrderWorkId, orderWorkDo.getId());
//        List<OrderWorkProductDo> workProductDoList = orderWorkProductDao.selectList(productWrapper);
//        if (!CollectionUtils.isEmpty(workProductDoList)) {
//            workProductDoList.forEach(orderWorkProductDo -> {
//                OrderWorkProductDetail productDetail = new OrderWorkProductDetail();
//                productDetail.setCount(orderWorkProductDo.getCount());
//                productDetail.setProductInfoId(orderWorkProductDo.getProductInfoId());
//                ProductInfoDo productInfoDo = productInfoDao.selectById(orderWorkProductDo.getProductInfoId());
//                if (productInfoDo != null) {
//                    productDetail.setProductName(productInfoDo.getProductName());
//                    productDetail.setCommoditySku(productInfoDo.getCommoditySku());
//                    DicBusinessItemDo businessItemDo2 = dicBusinessItemDao.selectById(productInfoDo.getProductType());
//                    if (businessItemDo2 != null) {
//                        productDetail.setProductType(businessItemDo2.getDicItemValue());
//                    }
//                    productDetail.setStorageSku(productInfoDo.getStorageSku());
//                }
//                productDetailList.add(productDetail);
//            });
//        }
//        res.setProductDetailList(productDetailList);
        return res;
    }


    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<OrderWorkDo> wrapper = queryBuild(map);
        IPage<OrderWorkDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletOrderWorkPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<AppletOrderWorkPageRes> handlerListAfter(List<OrderWorkDo> list) {
        List<AppletOrderWorkPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(orderWorkDo -> {
                AppletOrderWorkPageRes res = new AppletOrderWorkPageRes();
                res.setId(orderWorkDo.getId());
                res.setContent(orderWorkDo.getContent());
//                res.setSysTips(orderWorkDo.getSysTips());
                res.setWorkNumber(orderWorkDo.getWorkNumber());
                ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
                if (shopDo != null) {
                    res.setShop(shopDo.getShopName());
                }
                StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
                if (storageDo != null) {
                    res.setStorage(storageDo.getStorageName());
                }
                DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
                if (businessItemDo != null) {
                    res.setWorkType(businessItemDo.getDicItemValue());
                }
                res.setStatus(getStatusStr(orderWorkDo.getStatus()));
                result.add(res);
            });
        }
        return result;
    }

    private IPage<OrderWorkDo> pageInit(Map<String, String> map) {
        IPage<OrderWorkDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<OrderWorkDo> queryBuild(Map<String, String> map) {
        QueryWrapper<OrderWorkDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("work_number", map.get("keyword"));
        }
        if (StringUtils.isNotBlank(map.get("status"))) {
            wrapper.eq("status", map.get("status"));
        }
        if (StringUtils.isNotBlank(map.get("orderWorkType"))) {
            wrapper.eq("work_type", map.get("orderWorkType"));
        }
        if (StringUtils.isNotBlank(map.get("shopId"))) {
            wrapper.eq("shop_id", map.get("shopId"));
        }
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }


    private String getStatusStr(Integer type) {
        if (type == null) return "";
        else if (type == 0) return "未审核";
        else if (type == 1) return "已审核";
        else if (type == 2) return "已处理";
        else if (type == 3) return "已完成";
        else return "";
    }

}
